package com.zoshine.zzg.service;

import com.zoshine.zzg.dao.*;
import com.zoshine.zzg.exception.AppException;
import com.zoshine.zzg.exception.ErrorMessage;
import com.zoshine.zzg.pojo.*;
import com.zoshine.zzg.pojo.pojostatus.GoodsReturnStatus;
import com.zoshine.zzg.pojo.pojostatus.InvoiceStatus;
import com.zoshine.zzg.pojo.pojostatus.OrderStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author 田沛凌
 * @time 2017/8/3 13:25
 */
@Service
@Transactional
public class GoodsReturnService extends BaseService<GoodsReturn> {

    private PrisonerInfoDao prisonerInfoDao;
    private GoodsPublishDetailDao goodsPublishDetailDao;
    private InvoiceDao invoiceDao;
    private ReleaseSlipDao releaseSlipDao;
    private ReleaseSlipDetailDao releaseSlipDetailDao;
    private OrderPrisonerDao orderPrisonerDao;
    private OrderPrisonerDetailDao orderPrisonerDetailDao;
    private GoodsHistoryDetailDao goodsHistoryDetailDao;

    @Autowired
    public void setPrisonerInfoDao(PrisonerInfoDao prisonerInfoDao) {
        this.prisonerInfoDao = prisonerInfoDao;
    }

    @Autowired
    public void setGoodsPublishDetailDao(GoodsPublishDetailDao goodsPublishDetailDao) {
        this.goodsPublishDetailDao = goodsPublishDetailDao;
    }

    @Autowired
    public void setInvoiceDao(InvoiceDao invoiceDao) {
        this.invoiceDao = invoiceDao;
    }

    @Autowired
    public void setReleaseSlipDao(ReleaseSlipDao releaseSlipDao) {
        this.releaseSlipDao = releaseSlipDao;
    }

    @Autowired
    public void setReleaseSlipDetailDao(ReleaseSlipDetailDao releaseSlipDetailDao) {
        this.releaseSlipDetailDao = releaseSlipDetailDao;
    }

    @Autowired
    public void setOrderPrisonerDao(OrderPrisonerDao orderPrisonerDao) {
        this.orderPrisonerDao = orderPrisonerDao;
    }

    @Autowired
    public void setOrderPrisonerDetailDao(OrderPrisonerDetailDao orderPrisonerDetailDao) {
        this.orderPrisonerDetailDao = orderPrisonerDetailDao;
    }

    @Autowired
    public void setGoodsHistoryDetailDao(GoodsHistoryDetailDao goodsHistoryDetailDao) {
        this.goodsHistoryDetailDao = goodsHistoryDetailDao;
    }

    /**
     * 新增缺货单
     *
     * @param goodsReturn；
     */
    public void addGoodsReturn(GoodsReturn goodsReturn) {
        if (goodsReturn.getPrisonerId() == null) {
            throw new AppException(ErrorMessage.PRISONER_ID_CAN_NOT_NULL);
        }
        if (goodsReturn.getGoodsId() == null) {
            throw new AppException(ErrorMessage.GOODS_ID_CAN_NOT_BE_NULL_OR_UNDER_ZERO);
        }
        if (goodsReturn.getGoodsCount() == null || goodsReturn.getGoodsCount() <= 0) {
            throw new AppException(ErrorMessage.GOODS_RETURN_COUNT_CAN_NOT_NULL);
        }
        if (goodsReturn.getInvoiceId() == null) {
            throw new AppException(ErrorMessage.INVOICE_ID_CAN_NOT_BE_NULL);
        }
        Invoice invoice = invoiceDao.selectByPrimaryKey(goodsReturn.getInvoiceId());
        if (!(invoice.getStatus().equals(InvoiceStatus.RELEASE.getValue()) || invoice.getStatus().equals(InvoiceStatus.OUT_OF_STOCK.getValue()))) {
            throw new AppException(ErrorMessage.GOODS_RETURN_SHOULD_OUT_OR_RELEASE);
        }

        ReleaseSlip releaseSlip = releaseSlipDao.selectByInvoiceId(goodsReturn.getInvoiceId());
        ReleaseSlipDetail releaseSlipDetail = new ReleaseSlipDetail();
        releaseSlipDetail.setReleaseSlipId(releaseSlip.getId());
        List<ReleaseSlipDetail> releaseSlipDetails = releaseSlipDetailDao.selectList(releaseSlipDetail);
        for (ReleaseSlipDetail releaseSlipDetail1 : releaseSlipDetails) {
            if (releaseSlipDetail1.getGoodsHistoryId().equals(goodsReturn.getGoodsId())
                    && releaseSlipDetail1.getPrisonerId().equals(goodsReturn.getPrisonerId())) {
                //判断缺货数量是否大于发放单数量
                if (goodsReturn.getGoodsCount() > releaseSlipDetail1.getCount()) {
                    throw new AppException(ErrorMessage.GOODS_RETURN_CAN_NO_OUT_RELEASE_NUMBER);
                } else {
                    //修改点单详情收货数量
                    OrderPrisonerDetail orderPrisonerDetail = orderPrisonerDetailDao.selectByPrimaryKey(releaseSlipDetail1.getOrderPrisonerDetailId());
                    orderPrisonerDetail.setInvoiceCount(orderPrisonerDetail.getInvoiceCount() - goodsReturn.getGoodsCount());
                    orderPrisonerDetailDao.update(orderPrisonerDetail);
                    //修改点单收货数量
                    OrderPrisoner orderPrisoner = orderPrisonerDao.selectByPrimaryKey(orderPrisonerDetail.getOrderPrisonerId());
                    orderPrisoner.setTotalInvoice(orderPrisoner.getTotalInvoice() - goodsReturn.getGoodsCount());
                    if (orderPrisoner.getTotalInvoice() < orderPrisoner.getTotalCount()) {
                        orderPrisoner.setStatus(OrderStatus.PART_FINISH.getValue());
                    }
                    orderPrisoner.setUpdateTime(new Date());
                    orderPrisonerDao.update(orderPrisoner);
                    //修改发放单详情数量
                    releaseSlipDetail1.setCount(releaseSlipDetail1.getCount() - goodsReturn.getGoodsCount());
                    releaseSlipDetailDao.update(releaseSlipDetail1);
                    //生成缺货单
                    PrisonerInfo prisonerInfo = prisonerInfoDao.selectByPrimaryKey(goodsReturn.getPrisonerId());
                    GoodsHistoryDetail goodsHistoryDetail = goodsHistoryDetailDao.selectByPrimaryKey(goodsReturn.getGoodsId());
                    goodsReturn.setId(baseIdWorker.createId());
                    goodsReturn.setAccountPrisons(prisonerInfo.getOrgId());
                    goodsReturn.setGoodsId(goodsHistoryDetail.getGoodsId());
                    goodsReturn.setAccountSupplier(goodsHistoryDetail.getOrgId());
                    goodsReturn.setGoodsHistoryId(goodsHistoryDetail.getId());
                    goodsReturn.setStatus(GoodsReturnStatus.REPLENISHMENT.getValue());
                    goodsReturn.setCreateTime(new Date());
                    goodsReturn.setUpdateTime(new Date());
                    super.add(goodsReturn);
                    //改变发货单状态为缺货状态
                    invoice.setStatus(InvoiceStatus.OUT_OF_STOCK.getValue());
                    invoice.setUpdateTime(new Date());
                    invoiceDao.update(invoice);
                }
            }
        }
    }

    /**
     * 外网
     * 确认补货,
     *
     * @param idStr；
     */
    public void confirmBackGoods(String idStr) {
        //为空判断
        if (idStr == null || idStr.length() == 0) {
            throw new AppException(ErrorMessage.REQUEST_PARAM_CAN_NOT_NULL);
        }
        String[] pidArr = idStr.split(",");
        for (String id : pidArr) {
            //判断是否是待补货状态
            GoodsReturn goodsReturn = super.get(Long.parseLong(id));
            if (!(goodsReturn.getStatus().equals(GoodsReturnStatus.PENDING_BACK_GOODS.getValue()))) {
                throw new AppException(ErrorMessage.GOODS_RETURN_SHOULD_BE_PENDING_BACK_GOODS);
            }
            //状态变为待导入内网
            goodsReturn.setStatus(GoodsReturnStatus.FINISH_BACK_GOODS.getValue());
            super.edit(goodsReturn);
        }
    }

    /**
     * 内网
     * 确认收货,
     *
     * @param idStr；
     */
    public void confirmGoodsReturn(String idStr) {
        //为空判断
        if (idStr == null || idStr.length() == 0) {
            throw new AppException(ErrorMessage.REQUEST_PARAM_CAN_NOT_NULL);
        }
        String[] pidArr = idStr.split(",");
        for (String id : pidArr) {
            //判断是否是待收货
            GoodsReturn goodsReturn = super.get(Long.parseLong(id));
            if (!(goodsReturn.getStatus().equals(GoodsReturnStatus.PENDING_GOODS_RETURN.getValue()))) {
                throw new AppException(ErrorMessage.GOODS_RETURN_SHOULD_BE_AUDIT);
            }
            //状态变为待导入内网
            goodsReturn.setStatus(GoodsReturnStatus.FINISH_GOODS_RETURN.getValue());
            super.edit(goodsReturn);
            //改变发货单状态为已收货
            Invoice invoice = invoiceDao.selectByPrimaryKey(goodsReturn.getInvoiceId());
            invoice.setStatus(InvoiceStatus.CONFIRM.getValue());
            invoice.setUpdateTime(new Date());
            invoiceDao.update(invoice);
            ReleaseSlip releaseSlip = releaseSlipDao.selectByInvoiceId(goodsReturn.getInvoiceId());
            ReleaseSlipDetail releaseSlipDetail = new ReleaseSlipDetail();
            releaseSlipDetail.setReleaseSlipId(releaseSlip.getId());
            List<ReleaseSlipDetail> releaseSlipDetails = releaseSlipDetailDao.selectList(releaseSlipDetail);
            for (ReleaseSlipDetail releaseSlipDetail1 : releaseSlipDetails) {
                if (releaseSlipDetail1.getGoodsHistoryId().equals(goodsReturn.getGoodsHistoryId()) &&
                        releaseSlipDetail1.getPrisonerId().equals(goodsReturn.getPrisonerId())
                        ) {
                    //修改点单详情收货数量
                    OrderPrisonerDetail orderPrisonerDetail = orderPrisonerDetailDao.selectByPrimaryKey(releaseSlipDetail1.getOrderPrisonerDetailId());
                    orderPrisonerDetail.setInvoiceCount(orderPrisonerDetail.getInvoiceCount() + goodsReturn.getGoodsCount());
                    orderPrisonerDetailDao.update(orderPrisonerDetail);
                    //修改点单收货数量
                    OrderPrisoner orderPrisoner = orderPrisonerDao.selectByPrimaryKey(orderPrisonerDetail.getOrderPrisonerId());
                    orderPrisoner.setTotalInvoice(orderPrisoner.getTotalInvoice() + goodsReturn.getGoodsCount());
                    if (orderPrisoner.getTotalInvoice().equals(orderPrisoner.getTotalCount())) {
                        orderPrisoner.setStatus(OrderStatus.FINISHED.getValue());
                    }
                    orderPrisoner.setUpdateTime(new Date());
                    orderPrisonerDao.update(orderPrisoner);
                    //修改发放单详情数量
                    releaseSlipDetail1.setCount(releaseSlipDetail1.getCount() + goodsReturn.getGoodsCount());
                    releaseSlipDetailDao.update(releaseSlipDetail1);
                    //改变发货单状态为已生成发放单
                    invoice.setStatus(InvoiceStatus.RELEASE.getValue());
                    invoice.setUpdateTime(new Date());
                    invoiceDao.update(invoice);

                }
            }
        }

    }

}
